home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / tek / tekstor.c < prev    next >
Text File  |  1993-10-24  |  5KB  |  242 lines

  1. /* This has been rewritten to use one damn handle to hold all of the data. -- JMB */
  2.  
  3. #ifdef MPW
  4. #pragma segment TEKMAIN
  5. #endif
  6.  
  7. #include "TelnetHeader.h"
  8. #include "vgtek.proto.h"
  9. #include "rg0.proto.h"
  10. #include "rgmp.proto.h"
  11. #include "tekrgmac.proto.h"
  12. #include "tekdefs.h"    /* NCSA: sb - all defines are now here, for easy access */
  13. #include "tekstor.proto.h"
  14.  
  15. //    thiselnum runs from 0 to (handlesize-1), which is in accordance with C style
  16. //    arrays.  The handle is indexed 0...(handlesize-1) as well.  This is why you see
  17. //    the various +-1's. -- JMB 8/93
  18.  
  19. TEKSTOREP    newTEKstore(void)
  20. {
  21.     TEKSTOREP    s;
  22.     
  23.     s = (TEKSTOREP) NewPtr(sizeof(TEKSTORE));
  24.     if (s == NULL) return (NULL);
  25.     
  26.     if (!(s->dataHandle = NewHandle(0L)))
  27.         return (NULL);
  28.     
  29.     s->thiselnum = 0;
  30.     
  31.     return(s);
  32. }
  33.  
  34. void freeTEKstore(TEKSTOREP s)
  35. {
  36.     DisposeHandle(s->dataHandle);
  37.     DisposePtr((Ptr) s);
  38. }
  39.  
  40. short    addTEKstore(TEKSTOREP s, char d)
  41. {
  42.     Size    handlesize;
  43.     
  44.     handlesize = GetHandleSize(s->dataHandle);
  45.     SetHandleSize(s->dataHandle, ++handlesize);
  46.     if (MemError()) return (-1);
  47.     
  48.     (*(s->dataHandle))[handlesize-1] = d;    // Save in last position
  49.     
  50.     return(0);
  51. }
  52.  
  53. void    topTEKstore(TEKSTOREP s)
  54. {
  55.     s->thiselnum = 0;
  56. }
  57.  
  58. short    nextTEKitem(TEKSTOREP s)
  59. {
  60.     if (s->thiselnum == (GetHandleSize(s->dataHandle))) return(-1);    // At end of data
  61.     
  62.     return((short) ((*(s->dataHandle))[(s->thiselnum)++]) );
  63. }
  64.  
  65. short    TEKunstore(TEKSTOREP s)
  66. {
  67.     Size    handlesize;
  68.     
  69.     if ((handlesize = GetHandleSize(s->dataHandle)) == 0) return (-1);    // Nothing in store
  70.         
  71.     SetHandleSize(s->dataHandle, --handlesize);
  72.     if (MemError()) return(-1);
  73.     
  74.     if (s->thiselnum >= handlesize) s->thiselnum = handlesize - 1;
  75.     return(0);
  76. }
  77.  
  78. #ifdef    AARON_CONTORER_HAS_A_BRAIN        // Never true
  79. STOREP newstore(void)
  80. /* 
  81.     create a new, empty store and return a pointer to it.
  82.     Returns NULL if not enough memory to create a new store.
  83. */
  84. {
  85.     STOREP s;
  86.     
  87.     s=(STOREP) NewPtr((long) sizeof(STORE));            /* BYU LSC */
  88.     if (s==NULL) {
  89.         return(NULL);
  90.     }
  91.     else {
  92.         s->lasth = s->thish = s->firsth =
  93.             (HANDLEP) NewPtr((long) sizeof(HANDLE));    /* BYU LSC */
  94.         if (s->firsth==NULL) {
  95.             DisposPtr((Ptr) s);
  96.             return(NULL);
  97.         }
  98.         else {
  99.             s->firsth->pool = NewPtr((long) MINPOOL);    /* BYU LSC */
  100.             if (s->firsth->pool==NULL) {
  101.                 DisposPtr((Ptr) s->firsth);
  102.                 DisposPtr((Ptr) s);
  103.                 return(NULL);
  104.             }
  105.             else {
  106.                 s->lastelnum = s->thiselnum = -1;
  107.                 s->firsth->poolsize = MINPOOL;
  108.                 s->firsth->next = NULL;
  109.             }
  110.         }
  111.     }
  112.     return(s);
  113. }
  114.  
  115. void freestore(STOREP s)
  116. /*
  117.     Frees all pools and other memory space associated with store s.
  118. */
  119. {
  120.     HANDLEP h,h2;
  121.     h = s->firsth;
  122.     while (h != NULL) {
  123.         h2 = h;
  124.         DisposPtr(h->pool);
  125.         h = h->next;
  126.         DisposPtr((Ptr) h2);
  127.     }
  128.     DisposPtr((Ptr) s);
  129. }
  130.  
  131.  
  132. int addstore(STOREP s, char d)
  133. /*
  134.     Adds character d to the end of store s.
  135.     Returns 0 if successful, -1 if unable to add character (no memory).
  136. */
  137. {
  138.     int n; /* temp storage */
  139.     long size;                    /* BYU LSC */
  140.     HANDLEP h;
  141.  
  142.     n = ++(s->lastelnum);
  143.     size = s->lasth->poolsize;
  144.     if (n < s->lasth->poolsize) {
  145.         s->lasth->pool[n] = d;
  146.     }
  147.     else {
  148.         /* Pool full; allocate a new one. */
  149.         if (size<MAXPOOL) size <<= 1;
  150.         h = (HANDLEP)NewPtr((long) sizeof(HANDLE));        /* BYU LSC */
  151.         if (h==NULL) {
  152.             (s->lastelnum)--;
  153.             return(-1);
  154.         }
  155.         else {
  156.             h->pool = NewPtr(size);
  157.             if (h->pool==NULL) {
  158.                 DisposPtr((Ptr) h);
  159.                 (s->lastelnum)--;
  160.                 return(-1);
  161.             }
  162.             else {
  163.                 h->poolsize = size;
  164.                 h->next = NULL;
  165.                 s->lasth->next = h;
  166.                 s->lasth = h;
  167.                 s->lastelnum = 0;
  168.                 h->pool[0] = d;
  169.             }
  170.         }
  171.         } /* end of new pool allocation */
  172.     return(0);
  173. } /* end addstore() */
  174.  
  175. topstore(STOREP s)
  176. /*
  177.     Reset stats so that a call to nextitem(s) will be retrieving the
  178.     first item in store s.
  179. */
  180. {
  181.     s->thish = s->firsth;
  182.     s->thiselnum = -1;
  183. }
  184.  
  185. int nextitem(STOREP s)
  186. /*
  187.     Increment the current location in store s.  Then return the
  188.     character at that location.  Returns -1 if no more characters.
  189. */
  190. {
  191.     HANDLEP h;
  192.  
  193.     if (s->thish==s->lasth && s->thiselnum==s->lastelnum) return(-1);
  194.     else {
  195.         h = s->thish;
  196.         if (++(s->thiselnum) < s->thish->poolsize) {
  197.             return((int)(s->thish->pool[s->thiselnum]));
  198.             }
  199.         else {
  200.             /* move to next pool */
  201.             h = h->next;
  202.             s->thish = h;
  203.             s->thiselnum = 0;
  204.             return((int)(h->pool[0]));
  205.             }
  206.         }
  207. } /* end nextitem() */
  208.  
  209. int unstore(STOREP s)
  210. /*
  211.     Removes ("pops") the last item from the specified store.
  212.     Returns that item (in range 0-255), or returns -1 if there
  213.     are no items in the store.
  214. */
  215. {
  216.     HANDLEP nextolast;
  217.  
  218.     if (s->lastelnum > -1) { /* last pool not empty */
  219.         return((int)(s->lasth->pool[(s->lastelnum)--]));
  220.     } else { /* last pool empty */
  221.         if (s->lasth == s->firsth) return(-1);
  222.  
  223.         else { /* move back one pool */
  224.             nextolast = s->firsth;
  225.             while (nextolast->next != s->lasth)
  226.                 nextolast = nextolast->next;
  227.             DisposPtr((Ptr) nextolast->next);
  228.             s->lasth = nextolast;
  229.             s->lastelnum = nextolast->poolsize - 2;
  230.  
  231.             if (s->thish == nextolast->next) {
  232.                 s->thish = nextolast;
  233.                 s->thiselnum = s->lastelnum;
  234.             }
  235.  
  236.             nextolast->next = NULL;
  237.             return((int)(nextolast->pool[s->lastelnum+1]));
  238.         }
  239.     }
  240. }
  241. #endif
  242.